package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Coquan;
import model.dao.CoquanDAO;

/**
 * Servlet implementation class DonviServlet
 */
@WebServlet("/DonviServlet")
public class CoquanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CoquanServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		CoquanDAO coquanDAO = new CoquanDAO();

		/**
		 * Thêm mới cơ quan
		 * 
		 * @return boolean
		 * 
		 *         TriLD
		 */
		if (request.getParameter("them") != null) {

			String tencq = request.getParameter("tencq");
			String diachi = request.getParameter("diachi");
			String dienthoai = request.getParameter("dienthoai");
			String email = request.getParameter("email");

			int id = coquanDAO.getDonviID();
			boolean them = coquanDAO.themCoquan(id, tencq, diachi, dienthoai,
					email);

			request.setAttribute("them", them ? "true" : "false");
			RequestDispatcher rd = request
					.getRequestDispatcher("form_cqdieutra.jsp");
			rd.forward(request, response);
		}

		/**
		 * Hiển thị danh sách cơ quan
		 * 
		 * @return ArrayList<dsCoquan>
		 * 
		 *         TriLD
		 */
		else if (request.getParameter("dsCoquan") != null) {

			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();

			request.setAttribute("dsCoquan", dsCoquan);
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_coquan.jsp");
			rd.forward(request, response);

		} 
		
		/**
		 * Xóa 1 phòng ban
		 * 
		 * @return boolean
		 * 
		 *         TriLD
		 */
		else if(request.getParameter("xoa") != null) {
			
			String id = request.getParameter("xoa"); //request.getParameter("id");
			boolean xoa = coquanDAO.xoaCoquan(id);
			request.setAttribute("xoa",xoa ? "true":"false");
			
			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			request.setAttribute("dsCoquan", dsCoquan);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_coquan.jsp");
			rd.forward(request, response);
		}
		
		else if (request.getParameter("them_ds") != null) {
			
			RequestDispatcher rd = request
					.getRequestDispatcher("form_cqdieutra.jsp");
			rd.forward(request, response);
		} 
		
		else if(request.getParameter("sua") != null) {
			
			String id = request.getParameter("id");
			Coquan coquan = coquanDAO.getCoquan(id);
			
			request.setAttribute("coquan", coquan);
			

			RequestDispatcher rd = request
					.getRequestDispatcher("sua_coquan.jsp");
			rd.forward(request, response);
		}
		
		//cap nhat
		else if(request.getParameter("tencq2") != null) {
			
			String id = request.getParameter("idcq");
			String tencq = request.getParameter("tencq2");
			String diachi = request.getParameter("diachi");
			String dienthoai = request.getParameter("dienthoai");
			String email = request.getParameter("email");
			
			Coquan coquan = coquanDAO.getCoquan(id);
			
			request.setAttribute("coquan", coquan);
			
			boolean sua = coquanDAO.capNhatCQ(id, tencq, diachi, dienthoai, email);
			
			request.setAttribute("sua", sua ? "true":"false");
			
			RequestDispatcher rd = request
					.getRequestDispatcher("sua_coquan.jsp");
			rd.forward(request, response);
			
		}
		
		else {
			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();

			request.setAttribute("dsCoquan", dsCoquan);
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_coquan.jsp");
			rd.forward(request, response);
		}

	}

}
